<template>
  <div class="login">
    <div class="loginBox">
      <div class="avtor">
        <img
          src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-08a82cb9-d51c-4258-9073-90225f029164/ea81d032-848a-4ff6-a5d5-2ec50c2ee75b.jpg"
        />
      </div>
      <el-form
        label-width="80px"
        class="loginForm"
        ref="loginFormref"
        :model="formdata"
        :rules="rules"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="formdata.username"
            prefix-icon="iconfont icon-zhanghao"
          >
            placeholder="请输入用户名"
          </el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input 
          prefix-icon="iconfont icon-mima1"
          v-model="formdata.password" 
          placeholder="请输入密码" 
          type="password"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="buttons">
          <el-button @click="loginfn" type="primary">确定</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  props: [],
  components: {},
  data() {
    return {
        formdata:{
            username:'',
            password:''
        },
        rules:{
            username:[
                // require 必须输入
                // message 错误提示信息
                // trugger 触发方式
                {required:true,message:'请输入用户名',trigger:"blur"},
                {min:3,max:10,message:'请输入3-10个字符',trigger:"blur"}
            ],
            password:[
                 {required:true,message:'请输入密码',trigger:"blur"},
                 {min:6,max:10,message:'请输入6-10个字符',trigger:"blur"}
            ]
        }
    };
  },
  //方法 函数写这里
  methods: {
      resetForm(){
          console.log('resetForm')
          this.$refs.loginFormref.resetFields()
      },
      loginfn(){
          console.log('login')
          this.$refs.loginFormref.validate(async (valid)=>{
              console.log('valid',valid)
              if(!valid) return

            //   此时就是正确的情况，可以和数据库交互
            const res = await this.$http.get('/api/login',{
                params:this.formdata
            })
            if(res.data.code !=1){
                this.$message.error('登录失败');
                return
            }

            this.$message.success('登录成功')
            window.sessionStorage.setItem('token',res.data.token)
            this.$router.push('/home')
          })

      }
  },
  //计算属性
  computed: {},
  //侦听器
  watch: {},
  //过滤器
  filters: {},
  //以下是生命周期
  //组件创建之前
  beforeCreate() {},
  //组件创建之后
  created() {},
  //页面渲染之前
  beforeMount() {},
  //页面渲染之后
  mounted() {},
  //页面视图数据更新之前
  beforeUpdate() {},
  //页面视图数据更新之后
  updated() {},
  //页面销毁之前
  beforeDestroy() {},
  //页面销毁之后
  destroyed() {},
};
</script>


<style lang="scss"  scoped>
// vh:相对于视口的高度。视口被均分为100单位的vh
.login {
  background-color: darkcyan;
  height: 100vh;
}

.loginBox {
  width: 450px;
  height: 350px;
  background-color: #ffffff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
}

.avtor {
  height: 60px;
  width: 60px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 5px;
  box-shadow: 0 0 10px #dddddd;
  position: absolute;
  transform: translate(-15%, -15%);
  background-color: #ffffff;
}
.avtor img {
  height: 60px;
  width: 60px;
  border-radius: 50%;
  background-color: #eee;
}

.buttons {
  display: flex;
}

.loginForm {
  position: absolute;
  bottom: 30px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
</style>